//输入两个链表，找出它们的第一个公共结点。
struct ListNode
{
    int val;
    struct ListNode *next;
    ListNode(int x) :val(x), next(NULL) {}
};
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2)
{
        ListNode *p1 = pHead1;
        ListNode *p2 = pHead2;
        while(p1!=p2){
            p1 = (p1==NULL ? pHead2 : p1->next);
            p2 = (p2==NULL ? pHead1 : p2->next);
        }
        return p1;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    ListNode* p3 = new ListNode(1);//复习链表的指针、非指针建法，这里有三条链表
    
    ListNode a0(3),a1(6),a2(9);
    ListNode b0(4);
    ListNode *p1=&a0;ListNode *p2=&b0;
    a0.next=&a1;a1.next=&a2;
    b0.next=&a1;
    
    p3->next=&a2;
    
    ListNode* p4 = new ListNode(66);
    p4->next=p3;
    cout<<FindFirstCommonNode(p1, p2)->val<<endl;
    cout<<FindFirstCommonNode(p4, p2)->val<<endl;
    cout<<FindFirstCommonNode(p4, p2)->val<<endl;
    return 0;
}
